/** 
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * 
 *SdDataBoardCustomizeRecordController
 * SD数据看板自定义记录表
 * 
 * 修改纪录
 * 2021-12-23 版本：1.0 houyuhang 创建。
 * @version 版本：1.0
 * @author 作者：houyuhang
 * 创建日期：2021-12-23
 */

package com.topscomm.gpm.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.topscomm.basic.BasicEntity;
import com.topscomm.gpm.controller.auto.GpmDataBoardCustomizeRecordControllerAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.gpm.pojo.GpmDataBoardCustomizeRecordEntity;
import com.topscomm.gpm.service.impl.GpmDataBoardCustomizeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: houyuhang
 * @date: 2021-12-23
 * @description:SD数据看板自定义记录表Controller
 * @modify:
 */
@RestController
@RequestMapping("sd/dataBoardCustomizeRecord")
public class GpmDataBoardCustomizeRecordController extends GpmDataBoardCustomizeRecordControllerAuto implements IGpmDataBoardCustomizeRecordController {



	@Autowired
	private GpmDataBoardCustomizeRecordService gpmDataBoardCustomizeRecordService;

	/**
	 * @author: wudaliang
	 * @date: 2020-12-29
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_str")) {
			String value = paraMap.get("qry_str").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and (usercode like '%{0}%' or username like '%{0}%')", value);
			}
		}
		if (paraMap.containsKey("qry_recordlevel")) {
			String value = paraMap.get("qry_recordlevel").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and recordlevel = '{0}' ", value);
			}
		}
		if (paraMap.containsKey("qry_position")) {
			String value = paraMap.get("qry_position").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and position = '{0}' ", value);
			}
		}
		if (paraMap.containsKey("qry_role")) {
			String value = paraMap.get("qry_role").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and  EXISTS(select code from CBOROLE where CBOROLE.CODE=GpmDataBoardCustomizeRecord.ROLECODE and CBOROLE.NAME like '%{0}%')", value);
			}
		}
		if (paraMap.containsKey("qry_orgname")) {
			String value = paraMap.get("qry_orgname").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and EXISTS ( SELECT CODE  FROM CBOORG WHERE CBOORG.CODE = GpmDataBoardCustomizeRecord.ORGCODE AND CBOORG.NAME LIKE '%{0}%' ) ", value);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: wudaliang
	 * @date: 2020-12-29
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(
			@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}


	@Override
	@PostMapping(value = "/insert")
	public ResponseResult<Object> insert(
			@RequestBody(required = false) Map<String, Object> paraMap) {

		List<BasicEntity> entityList = new ArrayList<BasicEntity>();
		if (paraMap.containsKey("orgcode")) {
			GpmDataBoardCustomizeRecordEntity entity = new GpmDataBoardCustomizeRecordEntity();
			entity.setComponentid(ConvertUtil.convertToString(paraMap.get("componentid")));
			entity.setOrgcode(ConvertUtil.convertToString(paraMap.get("orgcode")));
			entity.setPosition(ConvertUtil.convertToString(paraMap.get("position")));
			entity.setRecordlevel("2");
			entity.setPriority("999");
			if(paraMap.containsKey("description")){
				entity.setDescription(ConvertUtil.convertToString(paraMap.get("description")));
			}
			entityList.add(entity);
			gpmDataBoardCustomizeRecordService.insertBatch(entityList);
		} else if (paraMap.containsKey("rolecode")) {
			GpmDataBoardCustomizeRecordEntity entity = new GpmDataBoardCustomizeRecordEntity();
			entity.setComponentid(ConvertUtil.convertToString(paraMap.get("componentid")));
			entity.setRolecode(ConvertUtil.convertToString(paraMap.get("rolecode")));
			entity.setPosition(ConvertUtil.convertToString(paraMap.get("position")));
			entity.setRecordlevel("1");
			entity.setPriority(ConvertUtil.convertToString(paraMap.get("priority")));
			if(paraMap.containsKey("description")){
				entity.setDescription(ConvertUtil.convertToString(paraMap.get("description")));
			}
			entityList.add(entity);
			gpmDataBoardCustomizeRecordService.insertBatch(entityList);
		} else {
			List<Map<String, Object>> userList = (List<Map<String, Object>>) paraMap.get("userList");
			for (Map<String, Object> user : userList) {
				GpmDataBoardCustomizeRecordEntity entity = new GpmDataBoardCustomizeRecordEntity();
				entity.setUsercode(ConvertUtil.convertToString(user.get("code")));
				entity.setUsername(ConvertUtil.convertToString(user.get("name")));
				entity.setComponentid(ConvertUtil.convertToString(paraMap.get("componentid")));
				entity.setPosition(ConvertUtil.convertToString(paraMap.get("position")));
				entity.setRecordlevel("0");
				entity.setPriority("0");
				if(paraMap.containsKey("description")){
					entity.setDescription(ConvertUtil.convertToString(paraMap.get("description")));
				}
				entityList.add(entity);
			}
			gpmDataBoardCustomizeRecordService.insertBatch(entityList);
		}
		return ResponseResult.ok(paraMap);
	}
}

